package com.jblv.frame.user.service.bll.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jblv.frame.common.enums.ExceptionEnum;
import com.jblv.frame.common.exception.FrameException;
import com.jblv.frame.common.type.BooleanType;
import com.jblv.frame.common.utils.UUIDGenerator;
import com.jblv.frame.user.api.bll.ISysMenuBLL;
import com.jblv.frame.user.api.entity.SysMenu;
import com.jblv.frame.user.api.params.SysMenuListParams;
import com.jblv.frame.user.api.service.ISysMenuService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 菜单业务逻辑类
 * @Author   yangtao
 * @Create   2020-05-26
 **/
@Component
@Service
public class SysMenuBLLImpl implements ISysMenuBLL {

    @Autowired
    private ISysMenuService sysMenuService;

    private static Logger logger = LoggerFactory.getLogger(SysMenuBLLImpl.class);


    @Override
    public SysMenu get(String id) {

//        String key = BaseCacheUtils.getBeanCacheKeyById(id);
        SysMenu bean = null;//BaseCacheUtils.get(key);
        if (bean != null) {
            return bean;
        }
        EntityWrapper<SysMenu> wrapper = new EntityWrapper<>();
        wrapper.eq("DELETE_FLAG", BooleanType.FALSE.getCode());
        wrapper.eq("MENU_ID",id);
        bean = sysMenuService.selectOne(wrapper);
        if (bean != null) {
            logger.info("add cache");
            //addCache(bean);
        }

        return bean;
    }


    @Override
    public String save(String creator, SysMenu sysMenu) {
        if (sysMenu == null) {
            return null;
        }
        if (StringUtils.isEmpty(sysMenu.getMenuId())) {
            sysMenu.setMenuId(UUIDGenerator.getUUID());
        }

        sysMenu.setCreator(creator);
        sysMenu.setCreateTime(System.currentTimeMillis());
        sysMenu.setDeleteFlag(BooleanType.FALSE.getCode());

        boolean save = false;
        try {
            save = sysMenuService.insert(sysMenu);
        }catch (FrameException e){
            logger.error("插入错误:{}",e.getMessage());
        }
        if (save) {
            //addCache(sysMenu);
            return sysMenu.getMenuId();
        }

        return null;
    }

    @Override
    public boolean update(String operator, SysMenu sysMenu) {
        SysMenu sysMenuCheck = get(sysMenu.getMenuId());
        if (sysMenuCheck == null) {
            throw new FrameException(ExceptionEnum.PARAM_ERROR);
        }
        if (sysMenu != null) {

            sysMenu.setUpdater(operator);
            sysMenu.setUpdateTime(System.currentTimeMillis());

            boolean flag = sysMenuService.updateById(sysMenu);
            if (flag) {
                //SysMenu newBean = sysMenuService.selectById(sysMenu.getId());
                //updateCache(oldUserInfo, newBean);
                return true;
            }
        }
        return false;
    }



    @Override
    public boolean remove(String operator, String id) {

        if (StringUtils.isEmpty(id)) {
            return false;
        }
        SysMenu sysMenu = get(id);
        if (sysMenu != null) {
            SysMenu sysMenuUpdate = new SysMenu();
            sysMenuUpdate.setDeleteFlag(BooleanType.TRUE.getCode());
            sysMenuUpdate.setUpdater(operator);
            sysMenuUpdate.setMenuId(id);
            Boolean flag = sysMenuService.updateById(sysMenuUpdate);
            if (flag) {
                //removeCache(bayonetInfo);
                return true;
            }
        }
        return false;
    }

    @Override
    public Page<SysMenu> getList(SysMenuListParams params) {
        EntityWrapper<SysMenu> wrapper = new EntityWrapper<>();
        wrapper.eq("DELETE_FLAG", BooleanType.FALSE.getCode());
        if (params.getCreateTimeBegin() != null) {
            wrapper.ge("CREATE_TIME", params.getCreateTimeBegin());
        }
        if (params.getCreateTimeEnd() != null) {
            wrapper.lt("CREATE_TIME", params.getCreateTimeEnd());
        }

        if (params.getName() != null){
            wrapper.like("NAME", params.getName());
        }

        wrapper.orderBy("CREATE_TIME", false);


        Page<SysMenu> page = sysMenuService.selectPage(new Page<SysMenu>(params.getPage(), params.getLimit()), wrapper);
        return page;
    }

    @Override
    public List<SysMenu> getList() {
        EntityWrapper<SysMenu> wrapper = new EntityWrapper<>();
        wrapper.eq("DELETE_FLAG", BooleanType.FALSE.getCode());
        wrapper.orderBy("CREATE_TIME", false);

        List<SysMenu> sysMenus = sysMenuService.selectList(wrapper);
        return sysMenus;
    }


    @Override
    public List<SysMenu> getTreeList() {
        List<SysMenu> returnDTO = new ArrayList();

        getTree("0",returnDTO);

        return returnDTO;
    }

    public void getTree(String parentId, List<SysMenu> sysMenus){
        List<SysMenu> listByParentId = getListByParentId(parentId);

        if(listByParentId.size()==0){
             return;
        }

        for (SysMenu sysMenu : listByParentId) {
            sysMenus.add(sysMenu);

            getTree(sysMenu.getMenuId(), sysMenus);

        }
    }

    @Override
    public List<SysMenu> getListByParentId(String parentId) {
        EntityWrapper<SysMenu> wrapper = new EntityWrapper<>();
        wrapper.eq("DELETE_FLAG", BooleanType.FALSE.getCode());
        if(parentId!=null) {
            wrapper.eq("PARENT_ID", parentId);
        }
        wrapper.orderBy("ORDER_NUM", true);
        wrapper.orderBy("CREATE_TIME", false);

        List<SysMenu> sysMenus = sysMenuService.selectList(wrapper);
        return sysMenus;
    }
}
